﻿package frEngine.core
{
    import __AS3__.vec.*;
    import flash.display3D.*;
    import flash.events.*;
    import flash.utils.*;

    public class FrIndexBuffer3D extends EventDispatcher
    {
        public var indexBuffer:IndexBuffer3D;
        public var _needUpdate:Boolean = true;
        public var numTriangles:int = -1;
        private var _indexBytes:ByteArray;
        private var _indexVector:Vector.<uint>;
        private var _vertexVector:Vector.<Number>;
        private var _parentFrSurface:FrSurface3D;

        public function FrIndexBuffer3D(param1:FrSurface3D)
        {
            this._parentFrSurface = param1;
            return;
        }// end function

        final public function get needUpdate() : Boolean
        {
            return this._needUpdate;
        }// end function

        public function toUpdate() : void
        {
            this._needUpdate = true;
            this.numTriangles = -1;
            if (this._parentFrSurface)
            {
                this._parentFrSurface.toUpdate();
            }
            return;
        }// end function

        public function contextEvent(param1:Context3D) : Boolean
        {
            if (!this._needUpdate)
            {
                return true;
            }
            if (this.indexBuffer)
            {
                this.indexBuffer.dispose();
                this.indexBuffer = null;
            }
            if (this._indexVector)
            {
                this.indexBuffer = param1.createIndexBuffer(this._indexVector.length);
                this.indexBuffer.uploadFromVector(this._indexVector, 0, this._indexVector.length);
                if (this.numTriangles == -1)
                {
                    this.numTriangles = this._indexVector.length / 3;
                }
                this._needUpdate = false;
                return true;
            }
            else
            {
                this.indexBuffer = param1.createIndexBuffer(this._indexBytes.length / 2);
                this.indexBuffer.uploadFromByteArray(this._indexBytes, 0, 0, this._indexBytes.length / 2);
                if (this.numTriangles == -1)
                {
                    this.numTriangles = this._indexBytes.length / 2 / 3;
                }
                this._needUpdate = false;
            }
            return true;
            ;
            return false;
        }// end function

        public function get indexVector() : Vector.<uint>
        {
            if (!this._indexVector)
            {
                this._indexVector = new Vector.<uint>;
                if (this._indexBytes)
                {
                    this._indexBytes.position = 0;
                    while (this._indexBytes.bytesAvailable > 0)
                    {
                        
                        this._indexVector.push(this._indexBytes.readUnsignedShort(), this._indexBytes.readUnsignedShort(), this._indexBytes.readUnsignedShort());
                    }
                }
            }
            return this._indexVector;
        }// end function

        public function set indexBytes(param1:ByteArray) : void
        {
            if (param1 == this._indexBytes)
            {
                return;
            }
            if (this._indexBytes)
            {
                this._indexBytes.clear();
            }
            this._indexBytes = param1;
            return;
        }// end function

        public function set indexVector(param1:Vector.<uint>) : void
        {
            this._indexVector = param1;
            this.toUpdate();
            return;
        }// end function

        public function dispose(param1:Boolean = false) : void
        {
            if (this.indexBuffer)
            {
                this.indexBuffer.dispose();
            }
            this.indexBuffer = null;
            var _loc_2:String = null;
            this._parentFrSurface = null;
            return;
        }// end function

    }
}
